#pragma once

#include <iostream>
#include <cstdarg>
#include <ctime>

namespace log
{
    enum LOG_LEVEL {
        _DEBUG,
        _INFO,
        _WARNING,
        _ERROR,
        _FATAL
    };

    const int STD_SIZE = 	0x400;
    const int LOG_SIZE = 	0x400;
    const int TIME_SIZE = 	0x40;

    const std::string gLevelMap[] = {
        "DEBUG",
        "INFO",
        "WARNI",
        "ERROR",
        "FATAL"
    };

    const std::string logFile = "./file.log";

    void logMessage(const uint8_t level, const char* format, ...)
    {
#ifndef DEBUG_SHOW
        if (LOG_LEVEL::_DEBUG == level)  return ;
#endif
        char stdBuffer[STD_SIZE];
        char logBuffer[LOG_SIZE];
        char formatTime[TIME_SIZE];
        time_t t = time(nullptr);
        struct tm *timeinfo = localtime(&t);
        strftime(formatTime, sizeof formatTime, "%Y-%m-%d %H:%M:%S", timeinfo);
        snprintf(stdBuffer, sizeof stdBuffer, "[%s][%s] ", gLevelMap[level].c_str(), formatTime);
        va_list aq;
        va_start(aq, format);
        vsnprintf(logBuffer, sizeof logBuffer, format, aq);
        va_end(aq);
#ifdef LOG_FILE
        auto fp = fopen(logFile.c_str(), "a");
        fprintf(fp, "%s%s\n", stdBuffer, logBuffer);
        fclose(fp);
#else
        printf("%s%s\n", stdBuffer, logBuffer);
#endif
    }
}
